package tut.mysql.load.conf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import tut.mysql.load.constants.ConfAgrs;

public class ConfTest implements ConfAgrs {

	int maxThreads = 10;
	int maxQueriesPerThread = 1000;
	public static String CONF_FILE = "/var/tmp/conf_test.txt";

	HashMap<String, String> confMap = new HashMap<String, String>();

	public ConfTest() {
		readConf();
	}

	public void readConf() {

		File file = new File(CONF_FILE);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));

			String text = "";

			// repeat until all lines is read
			while ((text = reader.readLine()) != null) {
				String[] list = text.split("=");
				if(list.length !=2) continue;
				confMap.put(list[0].trim(), list[1]);
			}

			int threads = Integer.parseInt(confMap.get(CLICK_THREADS));
			if(threads > maxThreads) confMap.put(CLICK_THREADS, String.valueOf(maxThreads));
			int queries = Integer.parseInt(confMap.get(CLICK_QUERIES));
			if(queries > maxQueriesPerThread) confMap.put(CLICK_QUERIES, String.valueOf(maxQueriesPerThread));

			threads = Integer.parseInt(confMap.get(DELIVER_THREADS));
			if(threads > maxThreads) confMap.put(DELIVER_THREADS, String.valueOf(maxThreads));
			queries = Integer.parseInt(confMap.get(DELIVER_QUERIES));
			if(queries > maxQueriesPerThread) confMap.put(DELIVER_QUERIES, String.valueOf(maxQueriesPerThread));

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public HashMap<String, String> getConfMap() {
		return this.confMap;
	}
}
